<?php
/**
 * 商品类型属性控制器
 * @author e5
 *
 */
class TypeAttrController extends AuthController{
	private $db;
	public function __init(){
		parent::__init();
		$this->db = K("TypeAttr");
	}
	/**
	 * 类型属性列表
	 */
	public function index() {
		$tid = Q("get.tid",0,"intval");
		$total = $this->db->where("vshop_type_tid={$tid}")->count();
		$page = new Page($total,6);
		//查询数据
		$tAttrData = $this->db->where("vshop_type_tid={$tid}")->limit($page->limit())->all();
		if ($total > 6){
			$pageData = $page->show();
		}else {
			$pageData = "";
		}
		$this->assign("tAttrData",$tAttrData);
		$this->assign("pageData",$pageData);
		$this->display("typeAttr");
	}
	/**
	 * 添加类型属性
	 */
	public function add() {
		if (IS_POST){
			if (!$this->db->addTypeAttr()) $this->error($this->db->error);
			$this->success("添加属性成功",U("index",array("tid"=>Q("post.tid"))));
		}
		$this->display("addTypeAttr");
	}
	/**
	 * 编辑类型属性
	 */
	public function edit() {
		if (IS_POST){
			if (!$this->db->editTypeAttr()) $this->error($this->db->error);
			$this->success("编辑属性成功",U("index",array("tid"=>Q("post.tid"))));
		}
		//获得原数据
		$taid = Q("get.taid",0,"intval");
		$taData = $this->db->where("taid={$taid}")->find();
		if (empty($taData)) $this->error("属性不存在");
		//获得所有类型数据
		$tData = K("Type")->all();
		$this->assign("taData",$taData);
		$this->assign("tData",$tData);
		$this->display("editTypeAttr");
	}
	/**
	 * 删除类型属性
	 */
	public function del(){
		$taid = Q("get.taid",0,"intval");
		//判断当前属性下是否有商品属性
		if (K("GoodsAttr")->where("vshop_typeattr_taid={$taid}")->count() > 0) $this->error("请先删除包含本属性的商品");
		if (!$this->db->where("taid={$taid}")->delete()) $this->error("删除属性失败");
		$this->success("删除属性成功",U("index",array("tid"=>Q("get.tid"))));
	}
}